<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiAdmin/layui/css/layui.css" media="all">
    <style>
        .layui-form-select dl {
            max-height: 120px;
        }
    </style>
</head>

<body>
    <div class="layui-form" id="popup-from" style="padding: 20px 30px 0 0;">
        <input type="text" name="cId" value="" style="display: none;" />
        <div class="layui-form-item layui-row layui-col-space20">
            <div class="layui-col-xs6">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                    <input type="text" name="cName" lay-verify="required" placeholder="请输入用户名" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">登录名</label>
                <div class="layui-input-block">
                    <input type="text" name="cUserName" lay-verify="required" placeholder="请输入登录名" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">年龄</label>
                <div class="layui-input-block">
                    <input type="text" name="cAge" id="txtAge" lay-verify="age|required|number" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">手机号码</label>
                <div class="layui-input-block">
                    <input type="text" name="cTel" id="txtTel" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-col-xs6">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="cGender" value="1" title="男" checked>
                    <input type="radio" name="cGender" value="2" title="女">
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">是否启用</label>
                <div class="layui-input-block">
                    <input type="radio" name="cEnabled" value="true" title="是" checked>
                    <input type="radio" name="cEnabled" value="false" title="否">
                </div>
            </div>

            <div class="layui-col-xs12">
                <label class="layui-form-label">地址</label>
                <div class="layui-input-inline">
                    <div id="cRegionCode"></div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block" id="UserRoles">
            </div>
        </div>
        <div class="layui-form-item layui-layout-admin layui-hide">
            <div class="layui-input-block">
                <div class="layui-footer" style="left: 0; text-align: right">
                    <button class="layui-btn" lay-filter="form-submit" id="form-submit" type="button" lay-submit>
                        立即提交
                    </button>
                </div>
            </div>
        </div>
    </div>
    <script src="../../layuiAdmin/layui/layui.js"></script>
    <script src="../../layuiAdmin/lib/extend/xm-select.js"></script>
    <script>
        layui.config({
            base: '../../layuiAdmin/', //静态资源所在路径
            version: top.layui.setter.version
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'form'], function () {
            var index = layui.index;
            var $ = layui.$,
                form = layui.form,
                admin = layui.admin
            var id = $.getParam("id");

            var xmAddress = xmSelect.render({
                el: '#cRegionCode',
                name: 'cRegionCode',
                tips: '请选择地址',
                radio: true,
                clickClose: true,
                toolbar: { show: true, },
                tree: {
                    show: true,
                    strict: false
                },
                filterable: true,
                height: '150px',
                on: function (data) {
                    var arr = data.arr;
                    if (arr.length > 0) {

                    }
                },
                //动态赋值
                data() {
                    var data = null;
                    admin.req({
                        type: 'get',
                        async: false,
                        url: '/api/Address/GetAllRegions',
                        success: function (r) {
                            if (r.code == 200) {
                                data = r.data;
                            }
                        }
                    });
                    return data;
                }
            });
            form.verify({
                tel: function (value) {
                    var reg = new RegExp("^[0-9]*$");
                    if (!reg.test(value) || value.length != 11) {
                        return '请输入正确的11位电话号码';
                    }
                },
                age: function (value) {
                    var reg = new RegExp("^[0-9]*$");
                    if (!reg.test(value)) {
                        return '请输入正确的年龄';
                    }
                    if (value > 200) {
                        return '年龄不能大于200岁';
                    }
                }
            });

            admin.req({
                type: 'get',
                async: false,
                url: '/api/Role/GetRoleList',
                success: function (r) {
                    if (r.code == 200) { }
                    var html = [];
                    layui.each(r.data, function (index, item) {
                        html.push('<input type="checkbox" name="Roles" value="' + item.value + '" title="' + item.label + '"  />');
                    })
                    $("#UserRoles").html(html.join(''));
                    form.render();
                    $("input[type='text']").attr("autocomplete", "new-password");
                }
            });
            //获取表单值
            if (id != "") {
                admin.req({
                    type: 'get',
                    url: "/api/user/GetUserByUserId?userId=" + id,
                    success: function (r) {
                        if (r.code == 200) {
                            xmAddress.setValue([r.data.cRegionCode]);
                            $("#popup-from").SetFormData(r.data);
                            $("input[name='cUserName']").attr("disabled", "disabled");
                            $("input[name='cUserName']").css("background", "#eeeeee");
                            layui.each(r.data.userRoles, function (index, item) {
                                $('#UserRoles input[value="' + item.roleId + '"]').prop('checked', 'checked')
                            })
                            form.render();
                            $("input[type='text']").attr("autocomplete", "new-password");
                        }
                    }
                });
            } else {
                $("input[type='text']").attr("autocomplete", "new-password");
            }
            //保存表单
            form.on('submit(form-submit)', function (data) {
                var field = data.field; //获取提交的字段
                field.cRegionName = xmAddress.getValue()[0]?.title;
                //提交
                var Roles = [];
                $('input[name="Roles"]:checked').each(function (index, item) {
                    Roles.push(item.value);
                })
                field.Roles = Roles;
                //提交
                admin.req({
                    type: 'post',
                    url: "/api/user/EditUser",
                    data: field,
                    success: function (data) {
                        if (data.code == 200) {
                            parent.layer.msg('操作成功!', { icon: 1, time: 1000 }, function () {
                                parent.layui.table.reload(parent.areaName);
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                        } else {
                            parent.layer.msg('操作失败!\n' + data.msg, { icon: 2, time: 1000 });
                        }
                    }
                });
            });
        })
    </script>
</body>

</html>